// 求min-max之间的随机整数
function rand(min, max) {
	return parseInt(Math.random() * (max - min + 1)) + min;
}

// 判断arr里面是否存在num这个元素
function has(arr, num) {
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] == num) {
			return true;
		}
	}
	return false;
}

// 获取页面滚动的距离
function getScroll() {
	return {
		top: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop, //向上滚动的距离
		left: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft //向左滚动的距离
	}
}

// 返回一个随机颜色: #12acfe
function color() {
	var str = "#";
	for (var i = 1; i <= 6; i++) {
		var num = rand(0, 15); //0-15之间的一个随机数字
		var s = num.toString(16);
		str = str + s;
	}
	return str;
}

// 封装一个方法用于获取指定元素的样式
function getStyle(dom, attr) {
	if (window.getComputedStyle) {
		// 非IE使用
		return window.getComputedStyle(dom, null)[attr]
	} else {
		// IE使用
		return dom.currentStyle[attr]
	}
}

// 去除字符串前后空格
function trim(str) {
	return str.replace(/(^\s+)|(\s+$)/g, "")
}
// 单属性缓动函数
function move(dom, attr, target, fn) {
	// var fn = function(){
	//     alert(1)
	// }
	clearInterval(dom.timer);
	dom.timer = setInterval(function() {
		if (attr == "opacity") {
			var current = parseInt(getStyle(dom, attr) * 100); //如果属性是透明度，要*100再取整
		} else {
			var current = parseInt(getStyle(dom, attr));
		}
		var speed = (target - current) / 10
		speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
		if (attr == "zIndex") {
			var next = target; //如果属性是zIndex,不需要缓动，一步到位
		} else {
			var next = current + speed;
		}

		if (next == target) {
			clearInterval(dom.timer);
			if (fn) {
				fn()
			}
		}
		if (attr == 'zIndex') {
			dom.style.zIndex = next
		} else if (attr == "opacity") {
			dom.style.opacity = next / 100; //非IE78
			dom.style.filter = "alpha(opacity=" + next + ")" //IE78

		} else {
			dom.style[attr] = next + "px"
		}
	}, 20)
}
// 多属性缓动函数
function animate(dom, obj, fn) {
	clearInterval(dom.timer);
	dom.timer = setInterval(function() {
		var flag = true;

		for (var attr in obj) {
			var target = obj[attr];
			// 1 获取当前值
			if (attr == "opacity") {
				var current = parseInt(getStyle(dom, attr) * 100);
			} else {
				var current = parseInt(getStyle(dom, attr));
			}
			// 2 计算速度
			var speed = (target - current) / 10;
			speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
			// 3 计算下一个位置
			var next = current + speed;
			if (next != target) {
				flag = false
			}
			// 4 定位元素
			if (attr == "opacity") {
				dom.style.opacity = next / 100;
				dom.style.filter = "alpha(opacity=" + next + ")";
			} else if (attr == "zIndex") {
				dom.style.zIndex = target;
			} else {
				dom.style[attr] = next + "px";
			}

		}

		if (flag == true) {
			clearInterval(dom.timer);
			if (fn) {
				fn()
			}
		}
	}, 30)
}

//轮播图距离
function animate1(obj, target, callback) {
	clearInterval(obj.timer)
	obj.timer = setInterval(function() {
		var step = (target - obj.offsetLeft) / 10;
		step = step > 0 ? Math.ceil(step) : Math.floor(step);
		if (obj.offsetLeft == target) {
			clearInterval(obj.timer);
			if (callback) {
				callback();
			}
		}
		obj.style.left = obj.offsetLeft + step + 'px';
	}, 20)
}
